package csx.study.demo.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MailUtils {

    @Autowired
    private JavaMailSender mailSender;

    public String sendMail(String toEmail, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1257150224@qq.com"); // 发件人邮箱
        message.setTo(toEmail);        // 收件人邮箱
        message.setSubject("邮箱注册"); // 邮件主题
        message.setText("您的验证码为：" + code); // 邮件内容

        try {
            mailSender.send(message);
            log.debug("邮箱发送成功");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return code;
    }
}